//
//  JJRNNetwork.m
//  ClaimPlatform_RN
//
//  Created by hamilyjing on 06/04/2018.
//  Copyright © 2018 jj. All rights reserved.
//

#import "JJRNBridge.h"

#import "JJReactNativeService.h"
#import "JJServiceManager.h"
#import "JJReactNativeModel.h"
#import "JJLog.h"

@implementation JJRNBridge

RCT_EXPORT_MODULE(JJRNBridge)

@end

@interface JJRNBridge (JJTool)
@end

@implementation JJRNBridge (JJTool)

/**
 *  日志打印
 */
RCT_EXPORT_METHOD(log:(id)message logLevel:(NSString *)logLevel)
{
  if ([(NSString *)message length] <= 0) {
    return;
  }
  
  switch (logLevel.integerValue) {
    case 0:
    {
      JJLogDebug(@"rnLog", @"%@", message)
    }
      break;
    case 1:
    {
      JJLogInfo(@"rnLog", @"%@", message)
    }
      break;
    case 2:
    {
      JJLogWarn(@"rnLog", @"%@", message)
    }
      break;
    case 3:
    {
      JJLogError(@"rnLog", @"%@", message)
    }
      break;
      
    default:
      break;
  }
}

@end

@interface JJRNBridge (JJUI)
@end

@implementation JJRNBridge (JJUI)

@end

@interface JJRNBridge (JJRNNetwork)
@end

@implementation JJRNBridge (JJRNNetwork)

RCT_EXPORT_METHOD(request:(NSString *)optionString
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  NSDictionary *options = [JJRNBridge dictionaryWithJsonString:optionString];
  [[self __reactNativeService:options] requestRNWithOptions:options networkSuccessResponse:^(JJReactNativeModel *object, id otherinfo)
   {
     NSString *resultSring = object.rnDataContentStr;
     if (resolve)
     {
       resolve(resultSring);
     }
     
   } networkFailResponse:^(NSError *error, id otherinfo)
   {
     if (reject)
     {
       NSString *message = @"网络连接超时，等会再试~";
       NSString *codeString = [NSString stringWithFormat:@"%ld", (long)error.code];
       reject(codeString, message, error);
     }
   }];
}

RCT_EXPORT_METHOD(cache:(NSString *)optionString
                  resolve:(RCTPromiseResolveBlock)resolve)
{
  NSDictionary *options = [JJRNBridge dictionaryWithJsonString:optionString];
  NSString *cacheObjString = [[self __reactNativeService:options] cacheRNWithOptions:options];
  if (resolve)
  {
    resolve(cacheObjString);
  }
}

RCT_EXPORT_METHOD(removeCache:(NSString *)optionString)
{
  NSDictionary *options = [JJRNBridge dictionaryWithJsonString:optionString];
  [[self __reactNativeService:options] removeCacheRNWithOptions:options];
}

RCT_EXPORT_METHOD(saveCache:(NSString *)optionString obj:(NSString *)objString)
{
  NSDictionary *options = [JJRNBridge dictionaryWithJsonString:optionString];
  [[self __reactNativeService:options] saveCacheRNWithOptions:options obj:objString];
}

- (JJReactNativeService *)__reactNativeService:(NSDictionary *)options {
  NSString *serviceName = options[@"serviceName"];
  if (!serviceName) {
    serviceName = @"JJReactNativeService";
  }
  
  JJReactNativeService *service = [[JJServiceFactory sharedServiceFactory] serviceWithServiceName:serviceName];
  return service;
}

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
  if (jsonString == nil) {
    return nil;
  }
  
  NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  NSError *err;
  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                      options:NSJSONReadingMutableContainers
                                                        error:&err];
  if(err) {
    NSLog(@"json解析失败：%@", err);
    return nil;
  }
  
  return dic;
}

+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
  if (nil == dic) {
    return nil;
  }
  NSError *parseError = nil;
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
  return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

@end
